﻿// CSpellManager.cs
// Author: Gant Briseno
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Casters.source.Objects.Spells;
using System.IO;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework;
#endregion

namespace Casters.source.Managers
{
    public class CSpellManager
    {
        protected List<CSpell> m_ListOfSpells;
        public int[] m_SelectedSpells = new int[6];

        static private CSpellManager instance = null;
        static public CSpellManager GetInstance()
        {
            if (instance == null)
                instance = new CSpellManager();
            return instance;
        }

        CSpellManager() { }

        /// <summary>
        /// Loads the spells
        /// </summary>
        /// <param name="filepath"></param>
        public void Load(string filename)
        {
            if (m_ListOfSpells != null)
                m_ListOfSpells = null;

            m_ListOfSpells = new List<CSpell>();

            string filepath = "Content/Spells/" + filename + ".bin";
            using (BinaryReader br = new BinaryReader(TitleContainer.OpenStream(filepath)))
            {
                int version = br.ReadInt32();
                int count = br.ReadInt32();
                for (int i = 0; i < count; ++i)
                {
                    CSpell spell = new CSpell();
                    spell.Load(br, version);
                    m_ListOfSpells.Add(spell);
                }
            }

            GetAccountInfo();
        }

        public void GetAccountInfo()
        {
            // Will setup current spell slots and which spells are unlocked
            // For now, just unlocks all spells
            for (int i = 0; i < m_ListOfSpells.Count; ++i)
                m_ListOfSpells[i].SetUnlocked(true);

            for (int i = 0; i < 6; ++i)
                m_SelectedSpells[i] = i;
        }

        public CSpell GetSpell(int i)
        {
            if(m_ListOfSpells.Count > 0 && i < m_ListOfSpells.Count && m_ListOfSpells[i] != null)
                return m_ListOfSpells[i];

            return null;
        }
    }
}
